'''
Created on 14.12.2009

@author: claudiugh
'''
import os 
import re 
import pstats 
import cProfile 

from apriori import AprioriRulesGenerator
from dh import DirectHashingGenerator
from parallel import ParallelRulesGenerator 
from zip import Dictionary

mail_dir = 'd:/work/school/data_mining/maildir/allen-p/inbox/'
connectors = ('your', 'me', 'will', 'at', 'be', 'that', 'where', 'when', 'by', 'can', 'i', 'a', 'as', 'we', 'have', 'are', 'you', 'it', 'on', 'if', 'this', 'is', 'then', 'or', 'and', 'to', 'the', 'in', 'for', 'of')
#connectors = () 

def parse_mail(filename):
    f = open(filename)
    words = set()
    header_re = re.compile("([a-zA-Z0-9\-]+:)\s(.+)") 
    for line in f.readlines(): 
        match = header_re.match(line)
        if not match:
            for item in line.split():
                word = item.strip(".()+-[]{},;").lower()
                if re.match("^[a-zA-Z]+$", word) and not word in connectors: 
                    words.add(word)
    return words 

def run_classic_apriori(transactions, minsupp, minconf):
    generator = AprioriRulesGenerator(transactions, minsupp, minconf)
    assoc_rules = generator.find_assoc_rules()
    #AprioriRulesGenerator.print_assoc_rules(assoc_rules)

def run_dh_apriori(transactions, minsupp, minconf):
    generator = DirectHashingGenerator(transactions, minsupp, minconf)
    assoc_rules = generator.find_assoc_rules()
    #DirectHashingGenerator.print_assoc_rules(assoc_rules)

def run_par_apriori(transactions, minsupp, minconf):
    generator = ParallelRulesGenerator(transactions, minsupp, minconf)
    assoc_rules = generator.find_assoc_rules()
    ParallelRulesGenerator.print_assoc_rules(assoc_rules)

MIN_SUPP = 0.15
MIN_CONF = 1
transactions = []
for root, dirs, files in os.walk(mail_dir): 
    for file in files: 
        transaction = parse_mail(os.path.join(root, file))
        transactions.append(transaction)
            
def test_par_apriori():
    run_par_apriori(transactions, MIN_SUPP, MIN_CONF)

def test_classic_apriori():
    run_classic_apriori(transactions, MIN_SUPP, MIN_CONF)

def test_dh_apriori():
    run_dh_apriori(transactions, MIN_SUPP, MIN_CONF)

if __name__ == '__main__':
    cProfile.run('test_par_apriori()', 'parprof')
    p = pstats.Stats('parprof')
    p.strip_dirs().sort_stats(-1).print_stats()
    #zip = Dictionary()
    #zip.add_db(transactions)
    #print 'Running direct-hashing apriori:'
    #run_dh_apriori(transactions, MIN_SUPP, MIN_CONF)
    
    
    #print 'Running classic apriori:'
    #run_classic_apriori(transactions, MIN_SUPP, MIN_CONF)        


    